//
//  BYNavigationBarView.m
//  BYControls
//
//  Created by 彬boy on 2018/11/14.
//  Copyright © 2018年 彬boy. All rights reserved.
//

#import "BYNavigationBarView.h"

@interface BYNavigationBarView ()

@property (nonatomic,strong)UIView *statuBarView;
@property (nonatomic,strong)UIView *contentView;

@end

@implementation BYNavigationBarView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self createNavUI];
    }
    return self;
}

- (void)createNavUI {
    
//    self.backgroundColor = [UIColor blackColor];
    [self addSubview:self.statuBarView];
    [self addSubview:self.contentView];
    
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.font = [UIFont boldSystemFontOfSize:20.0f];
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    self.navTitleView = titleLabel;
    
    UIButton *leftBtn = [[UIButton alloc] init];
    leftBtn.frame = CGRectMake(0.0f, 0.0f, 44.0f, 0.0f);
    
    [leftBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(leftBtnOnClick:) forControlEvents:UIControlEventTouchUpInside];
    [leftBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.navLeftBtn = leftBtn;
}

- (void)leftBtnOnClick:(UIButton *)sender {
    
    UIViewController *vc = [self getViewController];
    if (vc.navigationController) {
        if (vc.navigationController.viewControllers.count > 1) {
            [vc.navigationController popViewControllerAnimated:YES];
        }
    }
}

- (UIViewController *)getViewController {
    
    UIResponder *nextResponder = self;
    while (nextResponder) {
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)nextResponder;
        }else {
            nextResponder = nextResponder.nextResponder;
        }
    }
    return nil;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    CGFloat leftOrRightMargin = 10.0f;
    
    self.statuBarView.frame = CGRectMake(0.0f, 0.0f, self.bounds.size.width, StatusBarHeight);
    self.contentView.frame = CGRectMake(0.0f, StatusBarHeight, self.bounds.size.width, 44);
    self.navTitleView.frame = CGRectMake(10, 0, kScreenW-20, 44);
    
    CGFloat leftBtnHeight = _navLeftBtn.bounds.size.height != 0 ? _navLeftBtn.bounds.size.height : self.contentView.bounds.size.height;
    CGFloat leftBtnY = (self.contentView.bounds.size.height-leftBtnHeight)/2;
    self.navLeftBtn.frame = CGRectMake(leftOrRightMargin, leftBtnY, _navLeftBtn.bounds.size.width, leftBtnHeight);
    
    self.navRightBtn.frame = CGRectMake(kScreenW-50*kXX, 0.f, 40 , self.contentView.bounds.size.height);
        
    self.navRightView.frame = CGRectMake(kScreenW-80*kXX, 0.f, 40 , self.contentView.bounds.size.height);
    
    CGFloat leftViewHeight = _navLeftView.bounds.size.height != 0 ? _navLeftView.bounds.size.height : self.contentView.bounds.size.height;
    CGFloat leftViewY = (self.contentView.bounds.size.height-leftViewHeight)/2;
    
    self.navLeftView.frame = CGRectMake(leftOrRightMargin, leftViewY, _navLeftView.bounds.size.width, leftViewHeight);
}

- (void)setNavTitle:(NSString *)navTitle {
    
    _navTitle = navTitle;
    if (_navTitleView && [_navTitleView isKindOfClass:[UILabel class]]) {
        UILabel *titleLabel = ((UILabel *)_navTitleView);
        if (titleLabel) {
            titleLabel.text = navTitle;
            [titleLabel sizeToFit];
        }
    }
}

- (void)setNavTitleView:(UIView *)navTitleView {
    
    if (_navTitleView) {
        [_navTitleView removeFromSuperview];
        _navTitleView = nil;
    }
    _navTitleView = navTitleView;
    [self.contentView addSubview:_navTitleView];
}

- (void)setNavLeftView:(UIView *)navLeftView {
    if (!_navLeftView) {
        _navLeftView = navLeftView;
    }
    [self.contentView addSubview:_navLeftView];
}

- (void)setNavLeftBtn:(UIButton *)navLeftBtn {
    if (!_navLeftBtn) {
        _navLeftBtn = navLeftBtn;
    }
    [self.contentView addSubview:_navLeftBtn];
}

- (void)setNavRightBtn:(UIButton *)navRightBtn {
    if (!_navRightBtn) {
        _navRightBtn = navRightBtn;
    }
    [self.contentView addSubview:_navRightBtn];
}

- (void)setNavRightView:(UIView *)navRightView {
    if (!_navRightView) {
        _navRightView = navRightView;
    }
    [self.contentView addSubview:_navRightView];
}

- (void)setStatusBarBackgroundColor:(UIColor *)statusBarBackgroundColor {
    _statusBarBackgroundColor = statusBarBackgroundColor;
    self.statuBarView.backgroundColor = statusBarBackgroundColor;
}

- (UIView *)statuBarView {
    if (!_statuBarView) {
        _statuBarView = [[UIView alloc] init];
        _statuBarView.backgroundColor = [UIColor clearColor];
    }
    return _statuBarView;
}

- (UIView *)contentView {
    if (!_contentView) {
        _contentView = [[UIView alloc] init];
    }
    return _contentView;
}

@end
